package com.lyw.spring.demo.controller;

import com.lyw.spring.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {

    /**
     * 保证前端传的参数和后端接收的参数名字都一样
     * @param keyword
     * @return
     */
    @RequestMapping("/r1")
    //接收一个参数
    public String m1(String keyword) {
        return "接收参数: " + keyword;
    }

    @RequestMapping("/r2")
    //接收两个参数
    public String m2(String userName, String password) {
        return "用户名: userName:" + userName + " 密码: password:" + password;
    }

    @RequestMapping("/r3")
    //接收Integer类型参数
    public String r3(Integer number) {
        return "接收参数: " + number;
    }

    @RequestMapping("/r4")
    //接收int类型参数(不能被赋值给null)
    public String r4(int number) {
        return "接收参数: " + number;
    }

    @RequestMapping("/r5")
    //接收多个参数，封装成类
    public String r5(UserInfo userInfo) {
        return "接收参数: userInfo=" + userInfo.toString();
    }

    @RequestMapping("/r6")
    //从前端接收参数p并复制给keyword 参数绑定
    public String r6(@RequestParam(value = "q", required = false) String keyword) {
        return "接收参数: keyword=" + keyword;
    }

    @RequestMapping("/r7")
    //接收数组参数
    public String r7(String[] arr) {
        return "接收参数: arr=" + Arrays.toString(arr);
    }

    @RequestMapping("/r8")
    //接收集合参数 需要RequestParam注解进行参数绑定  数组不用注解是因为隐式的进行参数绑定
    public String r8(@RequestParam List<Integer> list) {
        return "接收参数: list=" + list.toString();
    }

    @RequestMapping("/r9")
    //接收json参数
    public String r9(@RequestBody UserInfo userInfo) {
        System.out.println(userInfo.getName());
        return userInfo.toString();
    }

    //从url中获取参数
    @RequestMapping("/article/{articleId}")
    public String r10(@PathVariable Integer articleId) {
        return "获取文章ID:" + articleId;
    }

    //从url中获取多个参数
    @RequestMapping("/article/{type}/{articleId}")
    public String r11(@PathVariable Integer articleId, @PathVariable("type") String articleType, @RequestParam String keyword) {
        return "获取文章ID:" + articleId + ",文章类型:" + articleType + ",关键字:" + keyword;
    }

    //上传文件
    @RequestMapping("r12")
    public String r12(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //文件上传
        file.transferTo(new File("D:\\视频\\" + file.getOriginalFilename()));
        return "文件上传成功";
    }

    //用请求对象获取cookie
    @RequestMapping("r13")
    public String r13(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }

        return "获取cookie成功";
    }

    //用注解获取cookie 一次只能获取一个cookie 可以写成多个参数但是太麻烦
    @RequestMapping("r14")
    public String r14(@CookieValue String java) {
        return "获取cookie成功:" + java;
    }

    //存储session
    @RequestMapping("setSession")
    public String setSession(HttpServletRequest request) {
        //从cookie中获取sessionId,根据sessionId获取session对象
        HttpSession session = request.getSession();//找不到会返回一个空的session对象不是null
        //默认存储在内存中
        session.setAttribute("userName", "张三");
        session.setAttribute("password", "123456");
        return "存储session成功";
    }

    //获取session
    @RequestMapping("getSession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(false);

        if(session == null) {
            return "用户未登录";
        }else {
            //从session中获取登录信息
            String userName = (String) session.getAttribute("userName");
            return "用户已登录:" + userName;
        }

    }

    @RequestMapping("getSession2")
    public String getSession2(HttpSession session) {
        //从session中获取登录信息
        String userName = (String) session.getAttribute("userName");
        return "用户已登录:" + userName;
    }

    @RequestMapping("getSession3")
    public String getSession3(@SessionAttribute("userName") String userName) {
        return "用户已登录:" + userName;
    }

    //获取请求header中的内容
    @RequestMapping("getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("user-agent");
        return "从header中获取userAgent成功: " + userAgent;
    }

    @RequestMapping("gerHeader2")
    public String getHeader2(@RequestHeader("user-agent") String userAgent) {
        return "从header中获取userAgent成功: " + userAgent;
    }

}
